package com.sc.admin.common.util;

import cn.hutool.core.text.StrFormatter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sc.admin.common.constant.StringPools;
import com.sc.admin.common.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author shen chen
 * @Date 2023-08-26
 * @Description 对象使用工具类
 */
@Slf4j
public final class ObjUtils extends ObjectUtils {


    private ObjUtils() {
    }

    public static boolean isNotEmpty(@Nullable Object[] array) {
        return !ObjectUtils.isEmpty(array);
    }

    public static boolean isNotEmpty(@Nullable Object obj) {
        return !ObjectUtils.isEmpty(obj);
    }

    public static boolean isNotNull(@Nullable Object obj) {
        return isNotEmpty(obj);
    }

    public static boolean isNotNull(@Nullable Object[] array) {
        return isNotEmpty(array);
    }

    public static boolean isNull(@Nullable Object[] array) {
        return isEmpty(array);
    }

    public static boolean isNull(@Nullable Object obj) {
        return isEmpty(obj);
    }

    /**
     * 判断所有的对象是否都为空
     * @param objArr objArr
     * @return 只要全部为空, 返回 true; 有任意一个不为空: false
     */
    public static boolean isAllEmpty(Object... objArr) {

        if (ObjectUtils.isEmpty(objArr)) {
            return true;
        }
        int length = objArr.length;
        for (Object object : objArr) {
            if (ObjectUtils.isEmpty(object)) {
                length--;
            }
        }
        return length <= 0;
    }

    /**
     * 判断所有的对象是否都不为空
     * @param objArr objArr
     * @return 只要有一个为空, 返回 true; 全部不为空 ,返回false
     */
    public static boolean isAnyEmpty(Object... objArr) {

        if (ObjectUtils.isEmpty(objArr)) {
            return true;
        }

        for (Object object : objArr) {
            if (ObjectUtils.isEmpty(object)) {
                return true;
            }
        }
        return false;
    }

    public static boolean equals(@Nullable Object o1, @Nullable Object o2) {
        return nullSafeEquals(o1, o2);
    }

    public static boolean notEquals(@Nullable Object o1, @Nullable Object o2) {
        return !nullSafeEquals(o1, o2);
    }

    /**
     * 对象转换
     *
     * @param source 数据来源实例
     * @param clazz  转换的class
     * @param <T>    泛型
     * @return 转换的class对象实例
     */
    public static <T> T copy(Object source, Class<T> clazz) {

        if (source == null) {
            return null;
        }

        T o = BeanUtils.instantiateClass(clazz);
        BeanUtils.copyProperties(source, o);
        return o;
    }

    /**
     * 对象转换
     *
     * @param source 数据来源实例
     * @param target  转换的实例
     */
    public static void copy(Object source, Object target) {

        if (source == null || target == null) {
            return;
        }

        BeanUtils.copyProperties(source, target);
    }

    /**
     * 对象集合转换
     *
     * @param source 数据来源实例集合
     * @param clazz  转换的class
     * @param <T>    泛型
     * @return 转换的class对象实例集合
     */
    public static <T> List<T> copyList(List<? extends Object> source, Class<T> clazz) {

        if (ObjectUtils.isEmpty(source)) {
            return new ArrayList<>();
        }

        List<T> voList = new ArrayList<>();

        for (Object o : source) {
            voList.add(copy(o, clazz));
        }
        return voList;
    }

    /**
     *  检查对象是否为空, 如果为空则扔出异常
     * @param obj 被检查的对象
     * @param msg 错误消息
     */
    public static void checkNotNull(Object obj, String msg) {
        if (isEmpty(obj)) {
            throw new BusinessException(msg);
        }
    }

    public static String format(String template, Object... params) {
        if (isEmpty(params) || isEmpty(template)) {
            return template;
        }
        return StrFormatter.format(template, params);
    }

    public static boolean isZero(Integer number) {
        return number == null || number == 0;
    }

    public static boolean isNotZero(Integer number) {
        return number != null && number == 0;
    }

    /**
     * 判断是否相等于任何一个
     * @param source 源对象
     * @param dest  需要比较的对象
     * @return t /f
     */
    public static boolean anyEquals(Object source, Object... dest) {
        for (Object d : dest) {
            if (source == d) {
                return true;
            }
        }
        return false;
    }

    public static String toJsonStr(Object obj) {
        String res = StringPools.EMPTY;
        if (obj == null) {
            return res;
        }
        try {
            ObjectMapper objectMapper = SpringBeanUtils.getBean(ObjectMapper.class);
            res = objectMapper.writeValueAsString(obj);
        } catch (Exception e) {
            log.info("PRO objMapper.writeValueAsString 异常，error：", e);
        }
        return res;
    }

    public static <T> T parseJson(String jsonStr, Class<T> clazz) {
        T res = null;
        if (isEmpty(jsonStr)) {
            return res;
        }
        try {
            ObjectMapper objectMapper = SpringBeanUtils.getBean(ObjectMapper.class);
            res = objectMapper.readValue(jsonStr, clazz);
        } catch (Exception e) {
            log.info("PRO objMapper.readValue 异常，error：", e);
        }
        return res;
    }
}
